package com.abvnet.project.water.project.controller;

import com.abvnet.common.utils.file.FileUploadUtils;
import com.abvnet.framework.aspectj.lang.annotation.Log;
import com.abvnet.framework.aspectj.lang.enums.BusinessType;
import com.abvnet.framework.config.AbvnetConfig;
import com.abvnet.framework.web.controller.BaseController;
import com.abvnet.framework.web.domain.AjaxResult;
import com.abvnet.framework.web.page.TableDataInfo;
import com.abvnet.project.water.project.domain.ProjectMicrobial;
import com.abvnet.project.water.project.service.IProjectMicrobialService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 菌剂信息操作处理
 *
 * @author duj
 * @date 2019-07-02
 */
@Controller
@RequestMapping("/project/projectMicrobial")
@Slf4j
public class ProjectMicrobialController extends BaseController {
    private String prefix = "project/projectMicrobial";

    @Autowired
    private IProjectMicrobialService projectMicrobialService;

    @RequiresPermissions("project:projectMicrobial:list")
    @GetMapping()
    public String projectMicrobial() {
        return prefix + "/projectMicrobial";
    }

    /**
     * 查询菌剂列表
     */
    @RequiresPermissions("project:projectMicrobial:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProjectMicrobial projectMicrobial) {
        startPage();
        List<ProjectMicrobial> list = projectMicrobialService.selectProjectMicrobialList(projectMicrobial);
        return getDataTable(list);
    }

    /**
     * 新增菌剂
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存菌剂
     */
    @RequiresPermissions("project:projectMicrobial:add")
    @Log(title = "菌剂", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("logoFile") MultipartFile logoFile,
                              @RequestParam("file") MultipartFile file,
                              ProjectMicrobial projectMicrobial) {
        try {
            if (logoFile != null && !logoFile.isEmpty()) {
                String logoUrl = FileUploadUtils.upload(AbvnetConfig.getMicroPath(), logoFile);
                projectMicrobial.setMicrobialLogoUrl(logoUrl);
            }
            if (file != null && !file.isEmpty()) {
                String microUrl = FileUploadUtils.upload(AbvnetConfig.getMicroPath(), file);
                projectMicrobial.setMicrobialUrl(microUrl);
            }
            return toAjax(projectMicrobialService.insertProjectMicrobial(projectMicrobial));
        } catch (Exception e) {
            log.error("添加菌剂失败！", e);
            return error(e.getMessage());
        }
    }

    /**
     * 修改菌剂
     */
    @GetMapping("/edit/{microbialId}")
    public String edit(@PathVariable("microbialId") Long microbialId, ModelMap mmap) {
        ProjectMicrobial projectMicrobial = projectMicrobialService.selectProjectMicrobialById(microbialId);
        mmap.put("projectMicrobial", projectMicrobial);
        return prefix + "/edit";
    }

    /**
     * 修改保存菌剂
     */
    @RequiresPermissions("project:projectMicrobial:edit")
    @Log(title = "菌剂", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestParam("logoFile") MultipartFile logoFile,
                               @RequestParam("file") MultipartFile file, ProjectMicrobial projectMicrobial) {
        try {
            if (logoFile != null && !logoFile.isEmpty()) {
                String logoUrl = FileUploadUtils.upload(AbvnetConfig.getMicroPath(), logoFile);
                projectMicrobial.setMicrobialLogoUrl(logoUrl);
            }
            if (file != null && !file.isEmpty()) {
                String microUrl = FileUploadUtils.upload(AbvnetConfig.getMicroPath(), file);
                projectMicrobial.setMicrobialUrl(microUrl);
            }
            return toAjax(projectMicrobialService.updateProjectMicrobial(projectMicrobial));
        } catch (Exception e) {
            log.error("修改菌剂失败！", e);
            return error(e.getMessage());
        }
    }

    /**
     * 删除菌剂
     */
    @RequiresPermissions("project:projectMicrobial:remove")
    @Log(title = "菌剂", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(projectMicrobialService.deleteProjectMicrobialByIds(ids));
    }

}
